/*
 *   Copyright (C) 2010  Jens Kieselbach
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.grootional.util

import java.sql.*
import javax.swing.*
import javax.swing.tree.*

import java.awt.Component

class DatabaseTreeCellRenderer implements TreeCellRenderer {

    TreeCellRenderer renderer

    def schemaIcon
    def tableIcon
    def typeUnknownIcon
    def typeTableIcon
    def typeSystemIcon
    def typeViewIcon

    def columnIcons = new Icon[36]
    def columnPkIcons = new Icon[36]

    DatabaseTreeCellRenderer(TreeCellRenderer tcr) {
        renderer = tcr
    }

    Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

        def comp = renderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus)

        switch(value.type) {
            case DatabaseNode.SCHEMA:
                comp.icon = schemaIcon
                break

            case DatabaseNode.TABLE:
                comp.icon = tableIcon
                break

            case DatabaseNode.TYPE_TABLE:
                comp.icon = typeTableIcon
                break

            case DatabaseNode.TYPE_SYSTEM:
                comp.icon = typeSystemIcon
                break

            case DatabaseNode.TYPE_VIEW:
                comp.icon = typeViewIcon
                break

            case DatabaseNode.TYPE_UNKNOWN:
                comp.icon = typeUnknownIcon
                break

            case DatabaseNode.COLUMN:
                comp.icon = value.primaryKey ? columnPkIcons[getDataTypIndex(value.dataType)] : columnIcons[getDataTypIndex(value.dataType)]
                break

            default:
                comp.icon = null
                break
        }

        comp
    }

    // --------------------------------------------------------------------------

    static int getDataTypIndex(int type) {

        switch(type) {
            default:
                return 0
            case Types.OTHER:
                return 0
            case Types.ARRAY:
                return 1
            case Types.BIGINT:
                return 2
            case Types.BINARY:
                return 3
            case Types.BIT:
                return 4
            case Types.BLOB:
                return 5
            case Types.BOOLEAN:
                return 6
            case Types.CHAR:
                return 7
            case Types.CLOB:
                return 8
            case Types.DATALINK:
                return 9
            case Types.DATE:
                return 10
            case Types.DECIMAL:
                return 11
            case Types.DISTINCT:
                return 12
            case Types.DOUBLE:
                return 13
            case Types.FLOAT:
                return 14
            case Types.INTEGER:
                return 15
            case Types.JAVA_OBJECT:
                return 16
            case Types.LONGNVARCHAR :
                return 17
            case Types.LONGVARBINARY:
                return 18
            case Types.LONGVARCHAR:
                return 19
            case Types.NCHAR:
                return 20
            case Types.NCLOB:
                return 21
            case Types.NULL:
                return 22
            case Types.NUMERIC:
                return 23
            case Types.NVARCHAR:
                return 24
            case Types.REAL:
                return 25
            case Types.REF:
                return 26
            case Types.ROWID:
                return 27
            case Types.SMALLINT:
                return 28
            case Types.SQLXML:
                return 29
            case Types.STRUCT:
                return 30
            case Types.TIME:
                return 31
            case Types.TIMESTAMP:
                return 32
            case Types.TINYINT:
                return 33
            case Types.VARBINARY:
                return 34
            case Types.VARCHAR:
                return 35
        }
    }
}

